#!/bin/bash -

OK=0
WARNING=1
UNKNOWN=2
CRITICAL=3

STATUS=$OK

PATH="/bin:/sbin:/usr/bin:/usr/sbin"

function send_mail(){
	title="$2"
	content="$1"
	echo "$content" | mutt -s "$title" biyujiang@ihep.ac.cn xujp@ihep.ac.cn
}


function check_osd(){
        timeout -k 5 5 ceph osd tree > /tmp/check_ceph.log
        if [[ $? -ne 0 ]]; then
		STATUS=$CRITICAL
		send_mail "Cannot access ceph now!"  "$(hostname): Ceph cannot be accessed!"
		exit $STATUS
	fi

	contents=$(awk '/down/ {print $4}' /tmp/check_ceph.log)
	if [[ "x$contents" != "x" ]]; then
		send_mail "Warning! Somne osds are down: $contents"  "$(hostname): OSDs of Ceph down!"
		STATUS=$WARNING
        fi
}


health=$(timeout -k 5 5 ceph health detail)

if [[ x$health == "x" ]]; then
	STATUS=$CRITICAL
	send_mail "Cannot access ceph now!"  "$(hostname): Ceph cannot be accessed!"
	exit $STATUS
fi

check_osd

if [[ $STATUS == "$OK" ]]; then
	echo "$(hostname -s): Ceph is OK"
else
	echo "$(hostname -s): Ceph is BAD"
fi

exit $STATUS
